from wtforms import Form
from wtforms.fields import StringField, PasswordField, IntegerField
from wtforms.validators import DataRequired, Length, EqualTo, ValidationError
from .base_validate import ValidateMixin
from app.constant import StoreStatus
from app.model.category_model import CategoryModel
from app.model.image_model import ImageModel


class CategoryPostValidate(Form, ValidateMixin):

    name = StringField(validators=[DataRequired(message='类型名不能为空')])
    img_id = IntegerField(validators=[DataRequired(message='类别图片不能为空且不能为非整数')])

    def validate_img_id(self, field):
        img_id = field.data
        if not img_id:
            raise ValidationError('类别图片id不能为空')
        img_id = [img_id]
        if not ImageModel.check_image(img_id):
            raise ValidationError('类别图片指定id不存在')
